//
//  GlobalDataHelper.swift
//  WineComing
//
//  Created by xiaozhi on 15/8/4.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit
import CoreLocation
let cartFileName = "cart.plist"

class GlobalDataHelper: NSObject {
    class var instance:GlobalDataHelper{
        struct Singleton {
            static let instance = GlobalDataHelper()
        }
        return Singleton.instance
    }
    class func getCartFromDisk()->[CartObj]{
        if Common.cartObjectFileExist(cartFileName).0{
            
        }
        return []
    }
    class func storeCart()->Bool{
        if Common.cartObjectFileExist(cartFileName).0{
            
        }
        return false
    }

    let geocoder = CLGeocoder()
    var currentLocation:String = ""
    var goodsInCart:[CartObj] = []
    var historyAddress:[String]? = NSUserDefaults.standardUserDefaults().stringArrayForKey("histroyAddress")
    var invoiceTitleHistory:[String]? = NSUserDefaults.standardUserDefaults().stringArrayForKey("invoiceTitleHistory") 
    var orderCreateInfo:orderCreateObj = ("","","","","","","","","","")
    var messageArray:[NSDictionary]? = NSUserDefaults.standardUserDefaults().arrayForKey("message") as? [NSDictionary]
    var searchHistroyArray:[String]? = NSUserDefaults.standardUserDefaults().stringArrayForKey("searchHistoryInfo")
    var tabCtl:MyTabBarController?
    var proviceObjArray:[ProviceObj]?
    var session:String?
    var userId:String?
    var userInfo:NSDictionary?
    var is_authentication:String?
    func hasPhoneNumber()->Bool{
        if self.getPhoneNumber() != nil{
            return true
        }
        return false
    }
    /************************************************
    * 消息
    ************************************************/
    func saveToMessage(info:NSDictionary!)->Void{
        if messageArray == nil {
            messageArray = []
            messageArray!.append(info)
            return
        }
        else{
            //超过5个删除最后一个
            if messageArray!.count >= 10{
                messageArray!.removeLast()
            }
            messageArray!.insert(info, atIndex: 0)
        }
    }
    func saveMessage()->Void{
        NSUserDefaults.standardUserDefaults().setObject(messageArray, forKey: "message")
        NSUserDefaults.standardUserDefaults().synchronize()
    }
    /************************************************
    * 搜索记录
    ************************************************/
    func saveToSearchHistroy(searchContent:String?)->Void{
        if searchContent == nil || searchContent == ""{
            return
        }
        if searchHistroyArray == nil {
            searchHistroyArray = []
            searchHistroyArray!.append(searchContent!)
            return
        }
        else{
            var exist = false
            for info in searchHistroyArray!{
                if info == searchContent{
                    exist = true
                    break
                }
            }
            if !exist{
                //超过5个删除最后一个
                if searchHistroyArray!.count >= 6{
                    searchHistroyArray!.removeLast()
                }
                searchHistroyArray!.insert(searchContent!, atIndex: 0)
            }
        }
        
    }
    func saveSearchHistory()->Void{
        if searchHistroyArray != nil{
            NSUserDefaults.standardUserDefaults().setObject(searchHistroyArray, forKey: "searchHistoryInfo")
            NSUserDefaults.standardUserDefaults().synchronize()
        }

    }
//    func getCurrentUserSearchHistoryList()->[String]{
//        var list:[String] = []
//        if userId != nil{
//            if let array = searchHistroyInfo?[self.userId!] as? [String]{
//                list = array
//            }
//        }
//        return list
//    }
    /************************************************
    * 订单状态 : 手机号码
    ************************************************/
    func getPhoneNumber()->String?{
        let phoneNumber:String? = NSUserDefaults.standardUserDefaults().objectForKey("phoneNumber") as? String
        return phoneNumber
    }
    func setPhoneNumber(phoneNumber:String)->Void{
        NSUserDefaults.standardUserDefaults().setObject(phoneNumber, forKey: "phoneNumber")
        NSUserDefaults.standardUserDefaults().synchronize()
    }
       /************************************************
    * 订单状态 : 定位地址
    ************************************************/
    func getPlace(location:CLLocation?)->String{
        let translateLocation = location?.transformToMars()
        if let place = translateLocation{
            geocoder.reverseGeocodeLocation(place, completionHandler: { (dataArray, error) -> Void in
//                if dataArray == nil || dataArray.count == 0{
//                    return
//                }
                self.currentLocation = ""
                let placeMark = dataArray![0] 
                let city = placeMark.locality
                let qu = placeMark.subLocality
                let street = placeMark.thoroughfare
                let number = placeMark.subThoroughfare
                if city != nil{
                    self.currentLocation = city!
                }
                if qu != nil{
                    self.currentLocation += qu!
                }
                if street != nil{
                    self.currentLocation += street!
                }
                if number != nil{
                    self.currentLocation += number!
                }
            })
        }
        return ""
    }
 }
